package cn.edu.zafu.bigdata.x.xiejunjie;

import java.util.Scanner;
import java.util.Random;

public class GuessNumberGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int maxRounds = 3; // 最多玩三轮
        int currentRound = 1; // 当前轮数

        while (currentRound <= maxRounds) {
            System.out.println("第 " + currentRound + " 轮游戏开始！");

            int secretNumber = random.nextInt(100) + 1; // 生成1到100之间的随机数
            int maxGuesses = 5; // 每轮最多猜测5次
            int guessCount = 0;

            while (guessCount < maxGuesses) {
                System.out.print("请输入你的猜测（1-100）:");
                int userGuess = scanner.nextInt();

                if (userGuess < 1 || userGuess > 100) {
                    System.out.println("输入超出范围，请重新输入！");
                    continue;
                }

                guessCount++;

                if (userGuess == secretNumber) {
                    System.out.println("恭喜你，猜对了！");
                    break;
                } else if (userGuess < secretNumber) {
                    System.out.println("太小了，再试试！");
                } else {
                    System.out.println("太大了，再试试！");
                }

                if (guessCount >= maxGuesses) {
                    System.out.println("很遗憾，没有次数了。正确答案是：" + secretNumber);
                }
            }

            // 询问用户是否想再玩一轮
            System.out.print("再来一局？(y/n): ");
            String answer = scanner.next();
            if (!answer.equalsIgnoreCase("y")) {
                break;
            }

            currentRound++;
        }

        scanner.close();
        System.out.println("感谢您的参与，再见！");
    }
}